package com.example.testapplication

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import kotlinx.coroutines.*

class MainActivity : AppCompatActivity() {
    companion object {
        private const val TAG = "MainActivity"
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        GlobalScope.launch {
            delay(1000)
            Log.d(TAG, "onCreate: GlobalScope.launch " + Thread.currentThread().name)
        }


//        runBlocking {
//            launch {
//                Log.d(TAG, "onCreate: runBlocking launch " + Thread.currentThread().name)
//            }
//
//            coroutineScope {
//                launch {
//                    delay(500)
//                    Log.d(TAG, "onCreate: coroutineScope launch " + Thread.currentThread().name)
//                }
//
//                delay(100)
//                Log.d(TAG, "onCreate: Task from coroutine scope")
//
//            }
//            Log.d(TAG, "onCreate: Coroutine scope is over")
//        }

        Log.d(TAG, "onCreate: ")
        runBlocking {
            var one = async { doSomeThingUsefulOne() }
            var two = async { doSomeThingUsefulTwo() }
            Log.d(TAG, "onCreate: ${one.await() + two.await()}")
        }
        Log.d(TAG, "onCreate: ")
        val one = doSomeThingUsefulOneAsync()
        val two = doSomeThingUsefulTwoAsync()
        runBlocking {
            Log.d(TAG, "onCreate: ${one.await() + two.await()}")
        }
    }

    fun doSomeThingUsefulOneAsync() = GlobalScope.async {
        doSomeThingUsefulOne()
    }

    fun doSomeThingUsefulTwoAsync() = GlobalScope.async {
        doSomeThingUsefulTwo()
    }

    suspend fun doSomeThingUsefulOne(): Int {
        delay(1000)
        return 13
    }

    suspend fun doSomeThingUsefulTwo(): Int {
        delay(500)
        return 12
    }
}
